<?php

namespace app\admin\controller\professionalqa;

use app\admin\model\UM as UM;
use app\admin\service\ProfessionalQaService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\Request as Request;

/**
 * ProfessionalQa 控制器
 * Class ProfessionalQa
 * @package app\professionalqa\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class ProfessionalQa
{
    private Request $request;

    private ProfessionalQaService $service;

    public function __construct(Request $request, ProfessionalQaService $professionalQaService)
    {
        $this->request = $request;
        $this->service = $professionalQaService;
    }

    /**
     * 列表
     * @param \app\admin\model\UM|null $user
     * @return ApiResponse
     * @url /professionalqa/professional_qa/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $isEnd = $request->param('isEnd', null);
        $categoryId = $request->param('categoryId', null);
        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);
        $kw = $request->param('kw', null);

        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $isEnd, $categoryId, $startTime, $endTime, $kw, $page, $page_size));
    }

    /**
     * 详情
     * @param \app\admin\model\UM|null $user
     * @return ApiResponse
     * @url /professionalqa/professional_qa/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 新增|修改
     * @param \app\admin\model\UM $user
     * @return ApiResponse
     * @url /professionalqa/professional_qa/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        if (!$request->has('id')) {
            $data = $this->service->add($user, $request->param());
        } else {
            $data = $this->service->modify($user, $request->param('id'), $request->param());
        }

        return ApiResponse::success($data);
    }

    /**
     * 删除
     * @param \app\admin\model\UM $user
     * @return ApiResponse
     * @url /professionalqa/professional_qa/delete
     */
    public function delete(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        $this->service->rm($user, $id);

        return ApiResponse::success(null);
    }
}
